home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / shift.def < prev    next >
Encoding:
Text File  |  1993-09-01  |  2.4 KB  |  96 lines

  1. This file is shift.def, from which is created shift.c.
  2. It implements the builtin "shift" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES shift.c
  23.  
  24. #if defined (HAVE_STRING_H)
  25. #  include <string.h>
  26. #else /* !HAVE_STRING_H */
  27. #  include <strings.h>
  28. #endif /* !HAVE_STRING_H */
  29.  
  30. #include "../shell.h"
  31.  
  32. $BUILTIN shift
  33. $FUNCTION shift_builtin
  34. $SHORT_DOC shift [n]
  35. The positional parameters from $N+1 ... are renamed to $1 ...  If N is
  36. not given, it is assumed to be 1.
  37. $END
  38.  
  39. /* Shift the arguments ``left''.  Shift DOLLAR_VARS down then take one
  40.    off of REST_OF_ARGS and place it into DOLLAR_VARS[9].  If LIST has
  41.    anything in it, it is a number which says where to start the
  42.    shifting.  Return > 0 if `times' > $#, otherwise 0. */
  43. int
  44. shift_builtin (list)
  45.      WORD_LIST *list;
  46. {
  47.   int times = get_numeric_arg (list);
  48.   int number, r;
  49.   WORD_LIST *args;
  50.  
  51.   if (!times)
  52.     return (EXECUTION_SUCCESS);
  53.  
  54.   if (times < 0)
  55.     {
  56.       builtin_error ("shift count must be >= 0");
  57.       return (EXECUTION_FAILURE);
  58.     }
  59.  
  60.   args = list_rest_of_args ();
  61.   number = list_length (args);
  62.   dispose_words (args);
  63.  
  64.   r = EXECUTION_SUCCESS;
  65.  
  66.   if (times > number)
  67.     {
  68.       times = number;
  69.       r = EXECUTION_FAILURE;
  70.     }
  71.  
  72.   while (times-- > 0)
  73.     {
  74.       register int count;
  75.  
  76.       if (dollar_vars[1])
  77.     free (dollar_vars[1]);
  78.  
  79.       for (count = 1; count < 9; count++)
  80.     dollar_vars[count] = dollar_vars[count + 1];
  81.  
  82.       if (rest_of_args)
  83.     {
  84.       WORD_LIST *temp = rest_of_args;
  85.  
  86.       dollar_vars[9] = savestring (temp->word->word);
  87.       rest_of_args = rest_of_args->next;
  88.       temp->next = (WORD_LIST *)NULL;
  89.       dispose_words (temp);
  90.     }
  91.       else
  92.     dollar_vars[9] = (char *)NULL;
  93.     }
  94.   return (r);
  95. }
  96.